package com.liming.auth.controller;

import com.liming.auth.service.*;
import com.liming.common.pojo.dto.authinfo.AddOrUpdateUserAddressDTO;
import com.liming.common.pojo.dto.authinfo.UserActivityAddDTO;
import com.liming.common.pojo.dto.authinfo.UsernameLoginDTO;
import com.liming.common.pojo.dto.authinfo.mini.UpdateUserInfoDTO;
import com.liming.common.pojo.dto.authinfo.mini.UpdateUserPwdDTO;
import com.liming.common.pojo.vo.authinfo.CaptchaVO;
import com.liming.common.pojo.vo.authinfo.mini.MiniAddressVO;
import com.liming.common.pojo.vo.authinfo.mini.MiniRegionVO;
import com.liming.common.pojo.vo.authinfo.mini.MiniUserInfoVO;
import com.liming.common.pojo.vo.dashboard.UserRegister;
import com.liming.common.result.LMResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author liming
 * @date 2025/1/8
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/service/user_auth_info")
@Tag(name = "用户权限信息-Service")
public class UserAuthMircoController {

    private final UserAuthService userAuthService;
    private final UserActivityService userActivityService;
    private final CaptchaService captchaService;
    private final UserAddressService userAddressService;
    private final RegionService regionService;

    @Operation(description = "获取验证码")
    @GetMapping("captcha")
    public LMResult<CaptchaVO> lineCaptcha() {
        return LMResult.success(captchaService.lineCaptcha());
    }

    @Operation(description = "用户注册统计")
    @GetMapping(value = "register_date_count")
    public LMResult<List<UserRegister>> registerDateCount(String scope) {
        return LMResult.success(userAuthService.registerDateCount(scope));
    }

    @Operation(description = "所有用户行为分数")
    @GetMapping(value = "activity_score")
    public LMResult<Map<Long, Double>> allUserActivityScore() {
        return LMResult.success("用户行为分数", userAuthService.activityScore());
    }

    @Operation(description = "根据时间获取活跃用户")
    @GetMapping(value = "activity_user_count")
    public LMResult<Integer> activityUserCount(@RequestParam String scope) {
        return LMResult.success("活跃用户数", userActivityService.activityUserCount(scope));
    }

    @Operation(description = "获取用户数量")
    @GetMapping(value = "userCount")
    public LMResult<Long> userCount() {
        return LMResult.success("用户数量", userAuthService.userCount());
    }

    @Operation(description = "用户性别数量")
    @GetMapping(value = "gender_quantity")
    public LMResult<Map<Integer, Long>> userGender() {
        return LMResult.success("用户性别数量", userAuthService.userGender());
    }

    @Operation(description = "获取系统所有用户ID和昵称")
    @GetMapping("all_user_id_nickname_map")
    public LMResult<Map<Long, String>> userIdsAndNickname() {
        return LMResult.success("系统所有用户ID", userAuthService.userIdsAndNickname());
    }

    @Operation(description = "获取系统所有用户ID和头像")
    @GetMapping("all_user_id_avatar_map")
    public LMResult<Map<Long, String>> userIdsAndAvatar() {
        return LMResult.success("系统所有用户ID和头像Map", userAuthService.idAndAvatarMap());
    }

    @Operation(description = "微信一键登录")
    @GetMapping("login_wx_mini")
    public LMResult<String> loginWithWxMini(@RequestParam String code) {
        return LMResult.success("登录成功", userAuthService.loginWithWxMini(code));
    }

    @Operation(description = "获取openid")
    @GetMapping("login_openid")
    public LMResult<String> loginOpenid() {
        return LMResult.success("openid", userAuthService.loginOpenid());
    }

    @Operation(description = "用户ID - 头像 Map")
    @PostMapping("id_avatar_map")
    public LMResult<Map<Long, String>> idAndAvatarMap(@RequestBody List<Long> userIds) {
        return LMResult.success(userAuthService.idAndAvatarMapByIds(userIds));
    }

    @Operation(description = "用户名登录")
    @PostMapping("login_username")
    public LMResult<String> loginWithUsername(@Validated @RequestBody UsernameLoginDTO usernameLoginDTO) {
        return LMResult.success("登录成功", userAuthService.loginWithUsername(usernameLoginDTO));
    }

    @Operation(description = "登出")
    @PostMapping("logout")
    public LMResult<Boolean> logout() {
        return LMResult.success("登出成功", userAuthService.logout());
    }

    @Operation(description = "新增 - 用户操作")
    @PostMapping("user_activity")
    public LMResult<Boolean> addUserActivity(@RequestBody UserActivityAddDTO dto) {
        return LMResult.success("新增用户行为日志成功", userActivityService.saveActivityLog(dto));
    }


    //
    @Operation(description = "列表 - 用户收货地址")
    @GetMapping("address")
    public LMResult<List<MiniAddressVO>> addressList() {
        return LMResult.success("用户收货地址列表", userAddressService.addressList());
    }

    @Operation(description = "Map - 区域")
    @GetMapping("region_map")
    public LMResult<Map<String, List<MiniRegionVO>>> miniRegionMap() {
        return LMResult.success("区域Map", regionService.miniRegionMap());
    }

    @Operation(description = "商品活动类型列表Map")
    @GetMapping("product/activity_type_list_map")
    public LMResult<Map<Long, List<Integer>>> productIdActivityTypeListMap() {
        return LMResult.success("商品活动类型列表Map", userActivityService.productIdActivityTypeListMap());
    }

    @GetMapping("product/activity_brand_category_list_map/{userId}/{code}")
    public LMResult<Map<Integer, List<Long>>> activityBrandAndCategoryListMap(@PathVariable Long userId, @PathVariable Integer code) {
        return LMResult.success(userActivityService.activityBrandAndCategoryListMap(userId, code));
    }

    @Operation(description = "小程序 - 用户信息")
    @GetMapping("info")
    public LMResult<MiniUserInfoVO> info() {
        return LMResult.success("用户信息", userAuthService.info());
    }

    @Operation(description = "根据用户地址ID,查询 联系人姓名/手机号/地址")
    @PostMapping("address/info")
    public LMResult<Map<Long, String>> addressInfoByIds(@RequestBody List<Long> userAddressIds) {
        return LMResult.success("地址信息Map", userAddressService.addressInfoByIds(userAddressIds));
    }

    @Operation(description = "新增|更新 - 用户收货地址")
    @PostMapping("address")
    public LMResult<Boolean> addOrUpdateAddress(@RequestBody AddOrUpdateUserAddressDTO addOrUpdateUserAddressDTO) {
        return LMResult.success("新增|更新用户收货地址成功", userAddressService.addOrUpdateAddress(addOrUpdateUserAddressDTO));
    }

    @Operation(description = "更新 - 用户信息")
    @PutMapping("info")
    public LMResult<Boolean> updateInfo(@RequestBody UpdateUserInfoDTO updateUserInfoDTO) {
        return LMResult.success("更新用户信息成功", userAuthService.updateInfo(updateUserInfoDTO));
    }

    @Operation(description = "更新 - 用户密码")
    @PutMapping("password")
    public LMResult<Boolean> updatePassword(@RequestBody UpdateUserPwdDTO updateUserPwdDTO) {
        return LMResult.success("更新用户密码成功", userAuthService.updatePassword(updateUserPwdDTO));
    }

    @Operation(description = "删除 - 用户收货地址")
    @DeleteMapping("address/{id}")
    public LMResult<Boolean> deleteAddress(@PathVariable Long id) {
        return LMResult.success("删除用户收货地址成功", userAddressService.removeById(id));
    }

}
